package com.dense.kuiniu.floatcabinet.mvp.manager.bord;

import android.util.SparseArray;

import org.litepal.LitePal;

import java.util.List;

/**
 * @author Administrator
 * @date 2020/11/17
 */
public class TaskManager {
    private SparseArray<Task> mRunningTask = new SparseArray<>();

    public TaskManager() {
        //从数据库查询未完成的任务
        List<Task> tasks = LitePal.findAll(Task.class);
        if (tasks != null && !tasks.isEmpty()) {
            for (Task task : tasks) {
                mRunningTask.put(task.getDoorNumber(), task);
            }
        }
    }

    public Task getRunTask(int doorNumber) {
        return mRunningTask.get(doorNumber);
    }

    public boolean addRunTask(String account, int doorNumber, int countType) {
        Task task = mRunningTask.get(doorNumber);
        if (task != null) {
            return false;
        }
        task = new Task(account, doorNumber, countType);
        task.saveOrUpdate();
        mRunningTask.put(doorNumber, task);
        return true;
    }

    public boolean hasTask(int doorNumber) {
        Task task = mRunningTask.get(doorNumber);
        return task != null;
    }

    public void removeRunTask(int doorNumber) {
        Task task = mRunningTask.get(doorNumber);
        if (task != null) {
            mRunningTask.remove(doorNumber);
            task.delete();
        }
    }

    public SparseArray<Task> getAllTask() {
        return mRunningTask;
    }

} 